package com.kady.Dao.impl;

import com.kady.Dao.TableDao;
import com.kady.entity.Table;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;
import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/3/26
 * \* Time: 19:13
 * \* 千峰JAVA2101班
 * \* Description: 餐桌DaoImpl
 * \
 */
public class TableDaoImpl extends BaseDao implements TableDao {

    /**
     * 查询餐桌列表
     */
    @Override
    public List<Table> selectTableList(String keyword) throws SQLException {
        String sql = "SELECT table_id tableId,\n" +
                "\ttable_name tableName,\n" +
                "\t`status`,\n" +
                "\tbooked_time bookedTime FROM t_table WHERE table_name LIKE ?;";
        List<Table> res = selectList(Table.class, sql, keyword);
        return res;
    }

    /**
     * 通过ID查询餐桌
     */
    @Override
    public Table selectTable(String tableId) throws SQLException {
        String sql = "SELECT table_id tableId,\n" +
                "\ttable_name tableName,\n" +
                "\t`status`,\n" +
                "\tbooked_time bookedTime FROM t_table WHERE table_id = ?;";
        Table res = selectOne(Table.class, sql, tableId);
        return res;
    }

    /**
     * 添加餐桌
     */
    @Override
    public void addTable(String tableName,byte status) throws SQLException {
        String sql = "INSERT INTO t_table (table_name,status) VALUES (?,?)";
        update(sql,tableName,status);
    }

    /**
     * 修改餐桌
     */
    @Override
    public void update(Object...objs) throws SQLException {
        String sql = "UPDATE t_table SET status = ?,booked_time = ? WHERE table_id = ? ";
        update(sql,objs);
    }

    /**
     * 删除餐桌
     */
    @Override
    public void deleteTable(String tableId) throws SQLException {
        String sql = "DELETE FROM t_table WHERE table_id = ?";
        update(sql,tableId);
    }

    /**
     * 通过状态查询餐桌列表
     */
    @Override
    public List<Table> selectTableListByStatus(byte status) throws SQLException {
        String sql = "SELECT table_id tableId,\n" +
                "\ttable_name tableName,\n" +
                "\t`status`,\n" +
                "\tbooked_time bookedTime FROM t_table WHERE status = ?;";
        List<Table> res = selectList(Table.class, sql, status);
        return res;
    }
}
